﻿<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
<head>
<%@ include file="/include/include.jsp" %>
<script type="text/javascript" src="<%=request.getContextPath()%>/static/js/map.js"></script>
<script>
	var map = new Map();
	 function Delivery(){
			this.pid;
			this.doNo;
			this.id;
			this.am;
			this.inverId;
			this.warehouseId;
			this.maintenanceStationId;
		}
		var ctx = "<%=request.getContextPath()%>"; 
		
		/**根据库存编码查找所有的货品*/
		function getInvertory(v,pid){
			
			if(v.checked){
				v.checked =false;
				var myAjax = new Ajax.Request(
					ctx+'/warehouse/stock/getwarehouse', // 请求的URL
					{
	                    method: 'post', // 使用GET方式发送HTTP请求
	                    parameters:'warehouseId='+v.value+'&pid='+pid+'&doNo='+$("deliveryOrderName").value,
	                    onComplete: showResponse // 指定请求成功完成时需要执行的方法
	                }
	            );
	        }else{
				var keys='';
				map.each(function(k,vv,i){
					if(vv.warehouseId == v.value){
						keys+= k +','; 
						var t1 =$("deliveryNumber"+vv.id).value;
						var tem =t1-vv.am; 
						if(tem == 0){//如果是等于0
							document.getElementById("detail"+vv.id).checked =false;
							document.getElementById("deliveryNumber"+vv.id).value="";
							$("deliveryNumber"+vv.id).title = parseInt($('number'+vv.id).value);
						}else{
							document.getElementById("deliveryNumber"+vv.id).value=tem;
							if($("deliveryNumber"+vv.id).title == '' || $("deliveryNumber"+vv.id).title == 0){
								$("deliveryNumber"+vv.id).title = vv.am;
							}else{
								$("deliveryNumber"+vv.id).title =parseInt($("deliveryNumber"+vv.id).title)+vv.am;	
								}	
						}						
					}
					
				});
				if(keys != ''){
					var keyArray = keys.substring(0,keys.length).split(',');
					for(var i =0;i<keyArray.length;i++){
						map.remove(keyArray[i]);
					}
				}
				document.getElementById("warehouseId"+v.value).checked=false;
		    }
		    
		}
		function showResponse(request) {
				//alert(request.responseText);
			
				var jsonData = eval("("+request.responseText+")");
				var detailIds = document.getElementsByName("details");
				var balanceNumbers = document.getElementsByName("balanceNumbers");
				var productCodes   =document.getElementsByName("productCodes"); 
				
				if(jsonData.length == 0){
					alert("没有库存，请选择其他仓储商！");
					return;
				}
				var house = 0;
				for(var i=0;i<jsonData.length;i++){
					var invenId = jsonData[i].id;
					var pcode = jsonData[i].pcode;
					var amount =jsonData[i].amount;
					var pid = jsonData[i].pid;
					var doNo = jsonData[i].doNo;
					var warehouseId = jsonData[i].warehouseId;
					house = warehouseId;
					for(var j=0,len=detailIds.length;j<len;j++){
						for(var k=0;k<len;k++){
							var id = balanceNumbers[k].title
							var co = productCodes[k].value;
							var am = balanceNumbers[k].value;
							if( (id == productCodes[k].title) && (id == detailIds[j].value)){
								var deliveryNumber;
								if($("deliveryNumber"+id)){
										deliveryNumber = $("deliveryNumber"+id).value;
										if(deliveryNumber == '')
										{
											deliveryNumber =0;
											
										}else{
											deliveryNumber = parseInt(deliveryNumber);
										}
								}
								var de = $("deliveryNumber"+id).title;
								if(pcode == co && deliveryNumber != de){
									if(amount > 0 && de  > 0){ //有库存的时候
											if(!map.containsKey(pcode+house)){
												var delivery = new Delivery();
												delivery.pid= pid;
												delivery.doNo = doNo;
												delivery.id = id;
												var number;
									
												if(amount >= de){
														if(deliveryNumber == 0  && de == am){
															number =am;
															document.getElementById("deliveryNumber"+id).value=number;
															$("deliveryNumber"+id).title = 0;	
														}else{
															number = de;
															document.getElementById("deliveryNumber"+id).value=am;
															$("deliveryNumber"+id).title = 0;	
														}
												}
												var count = 0;
												if(amount < de){
													number = de-(de-amount);
													document.getElementById("deliveryNumber"+id).value=number+deliveryNumber;
													$("deliveryNumber"+id).title = de - number;
												}
												delivery.am= number;
												delivery.invenId= invenId;
												delivery.warehouseId = warehouseId;
												map.put(co+house,delivery);
												detailIds[j].checked = true;
												document.getElementById("warehouseId"+warehouseId).checked= true;
											}
									}
								}
							}
						}
					}
				}
				var b = true;
				for(var i=0;i<jsonData.length;i++){
					if(jsonData[i].amount > 0){
						b =false;		
					}
				}
				if(b){
					document.getElementById("warehouseId"+house).checked=false;
					document.getElementById("warehouseId"+house).disabled =true;
					alert("库存不足，请选择其他的仓储商！");
				}
		}

		function submit_delivery(){
			if(map.size() ==0){
				alert("请选择仓储商！");
				return false;
			}
			var keySet = map.keySet();
			var jsonList =new Array();
			for(var i=0;i<keySet.length;i++)
			{
				var de = map.get(keySet[i]);
				de.doNo= $("deliveryOrderName").value;
				de.maintenanceStationId = $('maintenanceStationId').value;
				jsonList.push(JSON.stringify(de));	
			}
			$("jsonList").value='['+jsonList+']';
			return true;
		}
		function clearData(){
			map = new Map();
		}
		
</script>
</head>

<body>
	
	<%@ include file="/include/header.jsp" %>

	<%@ include file="/include/menu.jsp" %>
	
			
	<div id="mainContent">
		<h2><a href="${ctx}/${menu.url}">${menu.description}</a>&nbsp;-&nbsp;<a href="${ctx}/${empty currViewBarSubMenu_ ? submenu.url : currViewBarSubMenu_.url}">${empty currViewBarSubMenu_ ? submenu.description : currViewBarSubMenu_.description}</a></h2>
		
		<div class="tool-bar">
			<div title="${menu.description} - ${empty currViewBarSubMenu_ ? submenu.description : currViewBarSubMenu_.description} - 查询${empty currViewBarSubMenu_ ? submenu.description : currViewBarSubMenu_.description}"  href="${ctx}/purchase/purchasedetails/${empty purchaseOrder.id ? id : purchaseOrder.id}/search" class="modal-window modal-link">过滤查询</div>
			<c:if test="${purchaseOrder.percentage < 100}">
				<div href="${ctx}/purchase/related/${purchaseOrder.id}/add" class="modal-window modal-link" onclick="clearData()">就近库查询</div>
			</c:if>
		</div>
		
		<table cellspacing="0" cellpadding="0" class="form-grid">
			<tbody>
				<tr>
					<td>PO单号：</td>
					<td >${purchaseOrder.orderNumber}</td>
					<td>订单日期：</td>
					<td><fmt:formatDate value="${purchaseOrder.orderDate}" pattern="yyyy-MM-dd"/></td>
					
				</tr>
				<tr>
					<td>ASC代码：</td>
					<td>${purchaseOrder.maintenanceStation.number}</td>
					<td>ASC简称：</td>
					<td>${purchaseOrder.maintenanceStation.name}</td>
				</tr>
				<tr>
					<td>区域：</td>
					<td>${purchaseOrder.maintenanceStation.region.name}</td>
					<td>DO单号：</td>
					<td>
						<c:forEach items="${purchaseOrder.deliveryOrders}"  var="delivery">
							<c:if test="${purchaseOrder.percentage >= 100}">
								
										<a href="${ctx}/delivery/deliverydetails/${delivery.id}">	
											${delivery.number}
										</a>	
								
							</c:if>
							<c:if test="${purchaseOrder.percentage < 100}">
										<a href="${ctx}/delivery/deliverydetails/${delivery.id}">	
											${delivery.number}
										</a>	
							</c:if>
							<br/>
						</c:forEach>
							
					</td>
				</tr>
			</tbody>
		</table>
		
		<div class="grid-list">
			<table class="grid" cellspacing="0">
				<thead>
					<tr>
						<td>配件代码</td>
						<td>配件名称</td>
						<td>配件规格</td>
						<td>需求数量 </td>
						<td>匹配数量 </td>
						<!--<td>剩余数量</td>
						<td>百分比(%)</td>-->
					</tr>
				</thead>
				<tbody>
					<c:forEach var="purchaseDetail" items="${purchaseDetails}">
						<tr onmouseover="this.style.backgroundColor= '#A0CFEC'" onmouseout="this.style.backgroundColor= '#ffffff'" ${purchaseDetail.balance ? 'class="err"' : '' }>
							<td>${purchaseDetail.product.productCode}</td>
							<td>${purchaseDetail.product.productName}</td>
							<td>${detail.product.specification}</td>
							<td>${purchaseDetail.productNumber}</td>
							<td>${purchaseDetail.deliveryNumber}</td><!--
							<td>${purchaseDetail.balanceNumber}</td>
							<td>${purchaseDetail.percentage }</td>
						--></tr>
					</c:forEach>
				</tbody>
			</table>
			
			<t:page action="${ctx}/purchase/purchasedetails/${empty purchaseOrder.id ? id : purchaseOrder.id}"/>
		</div>
	</div>
	
	<%@ include file="/include/script.jsp" %>
	
</body>

</html> 